# usage in vim
# r !bash -ic 'snipcmake' 2>/dev/null <CR>

snipcmake(){
    cat<<'EOF'
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
project(<TODO>)

SET(src main.c)

SET(CMAKE_EXPORT_COMPILE_COMMANDS on)

INCLUDE_DIRECTORIES(include)

FUNCTION(add_executable target_name)
  #call builtin add_executable first
  _add_executable(${target_name} ${ARGN})
  #add custom statement
  EXECUTE_PROCESS(COMMAND ln -sf ${PROJECT_BINARY_DIR}/compile_commands.json ${PROJECT_SOURCE_DIR})
ENDFUNCTION(add_executable target_name)

ADD_EXECUTABLE(${PROJECT_NAME} ${src})

EOF
}


snipsh(){
cat<<-'EOF'
		#!/bin/bash
		rpath="$(readlink ${BASH_SOURCE})"
		if [ -z "$rpath" ];then
		    rpath=${BASH_SOURCE}
		fi
		thisDir="$(cd $(dirname $rpath) && pwd)"
		cd "$thisDir"

		user="${SUDO_USER:-$(whoami)}"
		home="$(eval echo ~$user)"
		
		red=$(tput setaf 1)
		green=$(tput setaf 2)
		yellow=$(tput setaf 3)
		blue=$(tput setaf 4)
		cyan=$(tput setaf 5)
		bold=$(tput bold)
		reset=$(tput sgr0)
		runAsRoot(){
		    verbose=0
		    while getopts ":v" opt;do
		        case "$opt" in
		            v)
		                verbose=1
		                ;;
		            \?)
		                echo "Unknown option: \"$OPTARG\""
		                exit 1
		                ;;
		        esac
		    done
		    shift $((OPTIND-1))
		    cmd="$@"
		    if [ -z "$cmd" ];then
		        echo "${red}Need cmd${reset}"
		        exit 1
		    fi
		
		    if [ "$verbose" -eq 1 ];then
		        echo "run cmd:\"${red}$cmd${reset}\" as root."
		    fi
		
		    if (($EUID==0));then
		        sh -c "$cmd"
		    else
		        if ! command -v sudo >/dev/null 2>&1;then
		            echo "Need sudo cmd"
		            exit 1
		        fi
		        sudo sh -c "$cmd"
		    fi
		}
		###############################################################################
        # write your code below (just define function[s])
		###############################################################################
		# TODO



		###############################################################################
		# write your code above
		###############################################################################
		help(){
		    cat<<EOF2
		Usage: $(basename $0) ${bold}CMD${reset}

		${bold}CMD${reset}:
		EOF2
	    perl -lne 'print "\t$2" if /^(function)?\s*?(\w+)\(\)\{$/' $(basename ${BASH_SOURCE})
		}

		case "$1" in
		     ""|-h|--help|help)
		        help
		        ;;
		    *)
		        "$@"
		esac

		EOF

}

snippy(){

cat<<-EOF
		#!/usr/bin/env python3
		# -*- coding: utf-8 -*-

		# import logging

		# LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
		# DATE_FORMAT = "%Y/%m/%d %H:%M:%S %p"
		# logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
		# logging.basicConfig(filename='my.log', level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)

		def main():
		    pass

		if __name__ == '__main__':
		    main()
	EOF
}

# vim: set ft=sh:
